﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;
using System.Globalization;

namespace QuanLyQuanCafe
{
    public partial class FrmKhuyenMai : Form
    {
        private KhuyenMaiBL khuyenmaiBL = new KhuyenMaiBL();
        private CT_KhuyenMaiBL ctkhuyenmaiBL = new CT_KhuyenMaiBL();
        private KhuVucBL khuvucBL = new KhuVucBL();
        public FrmKhuyenMai()
        {
            InitializeComponent();
        }

        private void FrmKhuyenMai_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = khuyenmaiBL.DSKhuyenMai();
            AnHienButton(false);
            dtNgayKT.Value = dtNgayBD.Value.AddDays(1);
            cbKV.DataSource = khuvucBL.DSKVBanHang();
            cbKV.DisplayMember = "TenKV";
            cbKV.ValueMember = "MaKV";
            tbGiam.Text = "0";
            btnXoa.Enabled = false;
        }

        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            //btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;
            btCongThuc.Enabled = flag;

            dtNgayBD.Enabled = flag;
            dtNgayKT.Enabled = flag;
            tbMoTaKM.Enabled = flag;
            dataGridView1.Enabled = !flag;
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbMaKM.Text = dataGridView1.Rows[row].Cells["MaKM"].Value.ToString();
                dtNgayBD.Text = string.Format("{0: dd/MM/yy}",dataGridView1.Rows[row].Cells["NgayBD"].Value.ToString());
                dtNgayKT.Text = string.Format("{0: dd/MM/yy}", dataGridView1.Rows[row].Cells["NgayKT"].Value.ToString());
                tbMoTaKM.Text = dataGridView1.Rows[row].Cells["MoTaKM"].Value.ToString();
                
            }
            catch
            {

            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            int makm = khuyenmaiBL.FindMa();
            tbMaKM.Text = makm.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Khuyến Mãi Từ Ngày [" + dtNgayBD.Text + "] Đến Ngày [" + dtNgayKT.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (khuyenmaiBL.XoaKM(tbMaKM.Text))
                    FrmKhuyenMai_Load(sender, e);
        }

        private string chuyendoi(string dt)
        {
            string date1 = dt.ToString();
            DateTime date = DateTime.ParseExact(date1, new string[] { "dd/MM/yy" }, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces);
            string date2 = string.Format("{0: MM/dd/yy}", date);
            dt = date2.ToString();
            return dt;
        }

        private KhuyenMaiBO LayThongTinKM()
        {
            KhuyenMaiBO khuyenmaiBO = new KhuyenMaiBO();
            khuyenmaiBO.MaKM = int.Parse(tbMaKM.Text);
            khuyenmaiBO.NgayBD = Convert.ToDateTime(chuyendoi(dtNgayBD.Text));
            khuyenmaiBO.NgayKT = Convert.ToDateTime(chuyendoi(dtNgayKT.Text));
            khuyenmaiBO.MoTaKM = tbMoTaKM.Text;
            return khuyenmaiBO;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                KhuyenMaiBO khuyenmaiBO = LayThongTinKM();
                if (khuyenmaiBL.CheckKhuyenMai(khuyenmaiBO.MaKM) )// && ctnhapkhoBL.CheckCTNhapKho(int.Parse(tbMaNhap.Text)))
                {
                    if (khuyenmaiBL.SuaKM(khuyenmaiBO))
                    {
                        FrmKhuyenMai_Load(sender, e);
                    }
                }
                else // if (ctnhapkhoBL.CheckCTNhapKho(int.Parse(tbMaNhap.Text)))
                {
                    if (khuyenmaiBL.ThemKM(khuyenmaiBO))
                        FrmKhuyenMai_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }

        private void btCongThuc_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = true;
            dataGridView2.DataSource = ctkhuyenmaiBL.DSCT_KM(tbMaKM.Text);
        }

        private void dataGridView2_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                cbKV.Text = dataGridView2.Rows[row].Cells["TenKV"].Value.ToString();
                string giam = (float.Parse(dataGridView2.Rows[row].Cells["Giam"].Value.ToString())*100).ToString();
                tbGiam.Text = giam;
            }
            catch
            {

            }
        }
        private CT_KhuyenMaiBO LayThongTinCT()
        {
            CT_KhuyenMaiBO ctkhuyenmaiBO = new CT_KhuyenMaiBO();
            ctkhuyenmaiBO.MaKM = int.Parse(tbMaKM.Text);
            ctkhuyenmaiBO.MaKV = int.Parse(cbKV.SelectedValue.ToString());
            if (tbGiam.Text == "" || tbGiam.Text == "0")
            {
                ctkhuyenmaiBO.Giam = 0;
                
            }
            else if(float.Parse(tbGiam.Text)/100 >0)
                ctkhuyenmaiBO.Giam = (float.Parse(tbGiam.Text) / 100);
            return ctkhuyenmaiBO;
        }
        private void btThemCT_Click(object sender, EventArgs e)
        {
            CT_KhuyenMaiBO ctkhuyenmaiBO = LayThongTinCT();
            if (!ctkhuyenmaiBL.CheckCTKV(int.Parse(cbKV.SelectedValue.ToString())))
            {
                if (ctkhuyenmaiBL.ThemCTKM(ctkhuyenmaiBO))
                {
                    dataGridView2.DataSource = ctkhuyenmaiBL.DSCT_KM(tbMaKM.Text);

                }
            }
        }

        private void btXoaCT_Click(object sender, EventArgs e)
        {
            if (ctkhuyenmaiBL.CheckCTKV(int.Parse(cbKV.SelectedValue.ToString())))
            {
                if (ctkhuyenmaiBL.XoaCTKM(cbKV.SelectedValue.ToString(), tbMaKM.Text))
                {
                    dataGridView2.DataSource = ctkhuyenmaiBL.DSCT_KM(tbMaKM.Text);

                }
            }
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = false;
        }
        /*
        private void btHuyCT_Click(object sender, EventArgs e)
        {
            tbGiam.Text = "0";
            btThemCT.Enabled = true;
            btXoaCT.Enabled = false; 
        }
        */
       
    }
}
